from selenium import webdriver
from selenium.webdriver.common.by import By
import datetime
import time

class Browser(object):
    def __init__(self) -> None:
        self.browser = webdriver.Edge()
        self.section_name = ""
        self.section_class = ""
        self.url = ""
        
    def url_get(self, url : str) -> bool:
        self.url = url
        try:
            self.browser.get(url)
            
            time.sleep(1) # 停留在jaccount界面时不要进入下一步，避免刷新二维码
            while True:
                elem = self.browser.find_elements(
                    by=By.XPATH, value="//span[contains(text(),'扫码登录')]")
                if len(elem) == 0:
                    break

            return True
        
        
        except:
            print("failed get url")
            return False
                
    
    def select_section(self, section_name : str, section_class : str, t:int=0.005):
        self.section_name = section_name
        self.section_class = section_class
        if section_name=="None":
            return
        Xpath = "//span[contains(text(),'" + section_name + "')"
        
        if section_class == "":
            Xpath += "]"
        else:
            # 以下未通过测试，还不能用，class只能是""现在
            Xpath += " and contains(@class,'" + section_class + "')]"
        
        n = 0
        while True:
            n += 1
            try:
                if n % 5 == 0 or True:
                    self.browser.refresh()
                section = self.browser.find_element(by=By.XPATH,
                                                    value=Xpath)
                section.click()
                print("section selected, n=", n)
                break
            except:
                if n % 100 == 0:
                    print("section not found, n=", n)
                time.sleep(t)
    
    
    def reserve(self, reserve_name:str="预约", reserve_class:list=["con_item_bt_btn","CAN_RESERVE"], t=0.005):
        Xpath = "//div[contains(text(),'" + reserve_name + "') \
            and contains(@class,'" + reserve_class[0] + "') \
            and contains(@class,'" + reserve_class[1] + "')]"
        
        n = 0
        while True:
            n += 1
            try:
                if n % 15 == 0:
                    self.browser.refresh()
                    self.select_section(self.section_name, self.section_class, 0.01)
                reserve_list = self.browser.find_elements(by=By.XPATH,
                                                            value=Xpath)
                if (len(reserve_list) != 0):
                    print("reserve found, n=", n)
                    reserve_list.reverse()
                    for item in reserve_list:
                        m = 0
                        try:
                            m += 1
                            item.click() # 全部点一遍后退出函数
                            print("clicked, m=", m)
                        except:
                            print("click failed, m=", m)
                    break
                else:
                    if n % 100 == 0:
                        print("reserve not found, n=", n)
                    time.sleep(t)
                    continue
            except:
                pass
                assert(0) # not possible here using find_elements()
    
    
    def confirm(self):
        n = 0
        Xpath1 = "//div[contains(text(),'确认') and contains(@class, 'bt_btn_rt')]]" # 严格查找条件
        Xpath2 = "//div[contains(@class, 'bt_btn_rt')]"
        
        print("here confirm")
        while True:
            n += 1
            try:
                confirm_btn = self.browser.find_element(by=By.XPATH, value=Xpath1)
                confirm_btn.click()
                print("confirm found!, n=", n)
                break;
            except:
                try:
                    confirm_btn = self.browser.find_element(by=By.XPATH, value=Xpath2)
                    confirm_btn.click()
                    print("confirm found!, n=", n)
                    break;
                except:
                    pass
                
                if n % 100 == 0:
                    print("confirm not found, n=", n)
                time.sleep(0.01)
                continue

            

